<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Admin extends Backend
{

    //构造函数
    public function __construct()
    {
        parent::__construct();

        //调用admin模型
        $this->AdminModel = model('Admin');
    }

    public function index()
    {

        ini_set("error_reporting", "E_ALL & ~E_NOTICE");

        //接收搜索的字段
        $keywords = $this->request->param('username');

        //定义一个条件数组
        $where = [];
        if (!empty($keywords)) {
            //有搜索的时候就显示搜索字段
            $placeholder = $keywords;
            $where['username|nickname'] = ['like', "%$keywords%"];
        } else {
            //没有搜索的时候就显示搜索字段
            $placeholder = '请输入要搜索的用户名';
        }


        //每页显示多少条数据
        $limit = 10;

        //查询数据总数
        $count = $this->AdminModel->count();
        $AdminList = $this->AdminModel->where($where)->order('id', 'asc')->paginate($limit);

        // $getdql =  $this->AdminModel->where($where)->order('id', 'asc')->fetchSql(true)->paginate($limit);
        // var_dump($getdql);

        //定义序号
        $start = ($AdminList->currentPage() - 1) * $AdminList->listRows();
        $this->assign([
            'count' => $count,
            'AdminList' => $AdminList,
            'start' => $start,
            'placeholder' => $placeholder,
        ]);


        return $this->fetch();
    }

    //添加页面
    public function add()
    {

        //是否有post提交
        if ($this->request->isPost()) {
            //接收所有的参数
            $data = $this->request->param();

            //设置密码盐
            $salt = build_ranstr(6);

            //加密
            $password = md5($data['pass'] . $salt);

            //把相对应的参数重新封装成新的数组

            $Data = [
                "username" => $data['username'],
                "nickname" => $data['nickname'],
                "password" => $password,
                "salt" => $salt,
                "state" => $data['state']
            ];

            //把数据插入数据表里面
            $result = $this->AdminModel->validate('common/Admin.add')->save($Data);
            if ($result == FALSE) {
                $this->error($this->AdminModel->getError());
                exit;
            } else {
                $this->success('添加成功', url('admin/admin/add'));
            }
        }

        return $this->fetch();
    }

    //修改管理员的状态
    public function state()
    {
        //获取ajax提交过来的id
        $id = $this->request->param('id', 0);

        //查询该id是否存在
        $admin = $this->AdminModel->find($id);
        if (!$admin) {
            $this->warning('该管理员不存在,请重新选择');
            exit;
        }

        //获取该管理员的状态
        $state = $this->request->param('title', '');

        $idx = 1;
        //如果是1:启用 0:禁用
        if ($state == "启用") {
            $idx = 0;
        } elseif ($state == "禁用") {
            $idx = 1;
        }

        //更新数据库状态字段
        // $result  = $this->AdminModel->fetchSql(true)->where('id', $id)->update(['state' => $idx]);
        $result  = $this->AdminModel->where('id', $id)->update(['state' => $idx]);

        if ($result == False) {
            $this->warning('修改失败');
            exit;
        } else {
            $this->finish('修改成功');
            exit;
        }
    }

    //编辑
    public function edit($id)
    {
        //根据传过来的id查询出数据
        $admin = $this->AdminModel->find($id);

        if (!$admin) {
            $this->error('该管理员不存在,请重新选择');
            exit;
        }

        if ($this->request->ispost()) {
            //从表单获取的所有数据
            $data = $this->request->param();

            //数据库根据id取出的密码盐
            $salt = $admin['salt'];

            //当用户有传入原密码或原密码不为空
            if (!empty($data['pass'])) {
                $password = md5($data['pass'] . $admin['salt']);

                //判断原密码与数据库保存的密码是否相同
                if ($admin['password'] != $password) {
                    $this->error('密码错误,请重新输入', null, $admin);
                    exit;
                }

                //判断表单两次输入的密码是否相同
                if ($data['newpass'] != $data['repass']) {
                    $this->error('密码不一致,请重新输入', null, $admin);
                    exit;
                }

                //重新生成一个新的密码盐
                $salt = build_ranstr(6);

                //新密码
                $newPass = md5($data['newpass'] . $salt);
            } else {
                $newPass = $admin['password'];
            }

            $Data = [
                'id' => $data['id'],
                'nickname' => $data['nickname'],
                'state' => $data['state'],
                'password' => $newPass,
                'salt' => $salt,
            ];

            $result  = $this->AdminModel->isUpdate(true)->save($Data);


            if ($result == FALSE) {
                $this->error('修改失败', null, $admin);
                exit;
            } else {
                $this->success('修改成功', url('admin/admin/list'));
                exit;
            }
        }



        $this->assign('admin', $admin);
        return $this->fetch();
    }

    //删除
    public function del()
    {

        if ($this->request->isAjax()) {
            $ids = $this->request->param('ids', 0);

            $result = $this->AdminModel->destroy($ids);

            if ($result === FALSE) {
                $this->warning('删除失败');
                exit;
            } else {
                $this->finish('删除成功');
                exit;
            }
        }
    }
}
